﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System.Linq ;
using DevExpress.Utils;

namespace DevEx
{
    public partial class ucHopDong : DevExpress.XtraEditors.XtraUserControl
    {
        public ucHopDong()
        {
            InitializeComponent();
        }

        private void ucCCL_Load(object sender, EventArgs e)
        {
            KhoiTaodtgHopDong("");
            KhoiTaoGiaoDien(true);
        }

        public void KhoiTaodtgHopDong(String Sname)
        {
            //CHopDong kho = new CHopDong();
            //dtgHopDong.DataSource = kho.SelectAllRecord();

            QLVTDataContext dc = new QLVTDataContext();
            var q = from p in dc.tblHopDongs select new { p.ID_HopDong , p.SoHopDong ,p.NgayKy ,p.TongGiaTri ,p.ID_DuAn };
            dtgHopDong.DataSource = q;
                                   
            //focus
            int focusrow = 0;
            if (Sname != "")
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    // if (dtcustomer.Rows[i]["CUST_NAME"].ToString().ToUpper() == Sname.ToUpper())
                    if (gridView1.GetRowCellValue(i, "SoHopDong").ToString().ToUpper() == Sname.ToUpper())
                    {
                        focusrow = i;
                    }
                }
            }         

            gridView1.FocusedRowHandle = focusrow;

            FormatdtgHopDong();
        }

        public void FormatdtgHopDong()
        {
            gridView1.Columns.ColumnByFieldName("ID_HopDong").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_DuAn").Visible = false;
            //gridView1.Columns.ColumnByFieldName("NgayHopDong")


            var cl_VT = gridView1.Columns.ColumnByFieldName("SoHopDong");
            cl_VT.Width = 150;
            cl_VT.Caption = "Số Hợp đồng";
            cl_VT.OptionsColumn.AllowEdit = false;
            cl_VT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NgayPhieu
            var cl_NgayPhieu = gridView1.Columns.ColumnByFieldName("NgayKy");
            cl_NgayPhieu.Width = 100;
            cl_NgayPhieu.Caption = "Ngày HĐ";
            cl_NgayPhieu.OptionsColumn.AllowEdit = false;
            cl_NgayPhieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NgayPhieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            
            //Columns TongGiaTri
            var cl_TongGiaTri = gridView1.Columns.ColumnByFieldName("TongGiaTri");
            //cl_TongGiaTri.Width = ;
            cl_TongGiaTri.Caption = "Tổng giá trị";
            cl_TongGiaTri.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_TongGiaTri.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_TongGiaTri.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_TongGiaTri.DisplayFormat.FormatString = "#,###.##";
            //Font f = new Font(this.Font, FontStyle.Bold);
            //cl_TongGiaTri.AppearanceCell.Font = f;
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtSoHopDong.EditValue = gridView1.GetFocusedRowCellValue("SoHopDong");
            txtGiaTri.EditValue = gridView1.GetFocusedRowCellValue("TongGiaTri");
            dtpNgayHopDong.EditValue = gridView1.GetFocusedRowCellValue("NgayHopDong");
        }


        Boolean fThem = false;
        Boolean fSua = false;
        CHopDong m_HopDong = new CHopDong();

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtSoHopDong.Focus();
            txtSoHopDong.Text = "";
            txtGiaTri.Text = "";
            dtpNgayHopDong.EditValue = null;
            fThem = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtSoHopDong.Focus();
            fSua = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            String SoHopDong = Convert.ToString(gridView1.GetFocusedRowCellValue("SoHopDong"));
            if (MessageBox.Show("Bạn muốn xóa hợp đồng này: " + SoHopDong, "Chú ý", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                int ID_HopDong = 0;
                ID_HopDong = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_HopDong"));
                m_HopDong.Delete(ID_HopDong);
                KhoiTaodtgHopDong("");
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            if (fThem)
            {
                tblHopDong HopDong = new tblHopDong();
                 HopDong.SoHopDong= txtSoHopDong.Text;
                 HopDong.TongGiaTri = Convert.ToDouble(txtGiaTri.Text);
                 HopDong.NgayKy = Convert.ToDateTime(dtpNgayHopDong.EditValue);
                m_HopDong.Insert(HopDong);
            }

            if (fSua)
            {
                tblHopDong HopDong = new tblHopDong();
                HopDong.ID_HopDong = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_HopDong"));
                HopDong.SoHopDong = txtSoHopDong.Text;
                HopDong.SoHopDong = txtSoHopDong.Text;
                HopDong.TongGiaTri = Convert.ToDouble(txtGiaTri.Text);
                m_HopDong.Update(HopDong);
            }

            KhoiTaoGiaoDien(true);
            KhoiTaodtgHopDong(txtSoHopDong.Text);
            fThem = false;
            fSua = false;

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(true);
            fThem = false;
            fSua = false;
        }

        public void KhoiTaoGiaoDien(Boolean f)
        {
            btnThem.Enabled = f;
            btnSua.Enabled = f;
            btnXoa.Enabled = f;
            btnGhi.Enabled = !f;
            btnHuy.Enabled = !f;
            txtSoHopDong.Properties.ReadOnly = f;
            txtGiaTri.Properties.ReadOnly = f;
            dtpNgayHopDong.Properties.ReadOnly = f;
        }
        
    }
}
